<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:s="http://jboss.com/products/seam/taglib"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
								xmlns:a4j="http://richfaces.org/a4j"
                xmlns:rich="http://richfaces.org/rich"
                template="layout/template.xhtml">
                       
<ui:define name="body">
		<s:remote include="favoritesAction"/>
		<script type="text/javascript">
var messages = null;
Seam.Remoting.eval("#" + "{messages}", function(value) {
	messages = value;
});

function addToFavorites(entityId, entityName, golferId) {
	var favoritesAction = Seam.Component.getInstance("favoritesAction");	
	favoritesAction.isFavorite(entityId, entityName, function(exists) {
		//Seam.Remoting.getContext().setConversationId(null);
		if (exists) {
			alert("This " + entityName + " is already a favorite");
		}
		else {
			var currentGolfer = Seam.Remoting.createType("org.open18.model.Golfer");
			currentGolfer.setId(golferId);
			var favorite = Seam.Component.newInstance("newFavorite");
			favorite.setEntityId(entityId);
			favorite.setEntityName(entityName);
			favorite.setGolfer(currentGolfer);
			favoritesAction.addFavorite(favorite, reportResult);
		}
	});
}

function reportResult(favoriteInstance) {
	var message = messages.get("favorite.added");
	message = message.replace("{0}", favoriteInstance.getEntityName());
	message = message.replace("{1}", favoriteInstance.getId());
	alert(message);
}

function removeAllFavorites() {
	Seam.Component.getInstance('favoritesAction').removeAllFavorites(function() {
		alert('nuked');
	});
}

function showFavoriteCourses(golferId) {
	var currentGolfer = Seam.Remoting.createType("org.open18.model.Golfer");
	currentGolfer.setId(golferId);
	Seam.Component.getInstance("favoritesAction").getFavoriteCourses(currentGolfer, function(result) {
		var favs = "";
		result.each(function(course) {
			favs += course.getName() + " (" + course.getFacility().getName() + " )\n";
		});
		alert(favs);
	});
}
		</script>
    
    <h:messages globalOnly="true" styleClass="message" errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg" id="globalMessages"/>
    
		<p>
		<a href="javascript: void(0);" onclick="">Delete favorites</a>
		|
		<a href="javascript: void(0);" onclick="showFavoriteCourses(#{currentGolfer.id});">Show favorite coures</a>
		</p>

    <rich:panel>
        <f:facet name="header">Course</f:facet>



        <s:decorate id="description" template="layout/display.xhtml">
            <ui:define name="label">description</ui:define>
            #{courseHome.instance.description}
        </s:decorate>


        <s:decorate id="designer" template="layout/display.xhtml">
            <ui:define name="label">designer</ui:define>
            #{courseHome.instance.designer}
        </s:decorate>


        <s:decorate id="fairways" template="layout/display.xhtml">
            <ui:define name="label">fairways</ui:define>
            #{courseHome.instance.fairways}
        </s:decorate>


        <s:decorate id="greens" template="layout/display.xhtml">
            <ui:define name="label">greens</ui:define>
            #{courseHome.instance.greens}
        </s:decorate>


        <s:decorate id="name" template="layout/display.xhtml">
            <ui:define name="label">name</ui:define>
            #{courseHome.instance.name}
						(<a href="#{facesContext.externalContext.requestContextPath}/course/view/#{courseHome.instance.restfulUrlHandle}">permalink</a>)
        </s:decorate>


        <s:decorate id="numHoles" template="layout/display.xhtml">
            <ui:define name="label">numHoles</ui:define>
            #{courseHome.instance.numHoles}
        </s:decorate>


        <s:decorate id="signatureHole" template="layout/display.xhtml">
            <ui:define name="label">signatureHole</ui:define>
            #{courseHome.instance.signatureHole}
        </s:decorate>


        <s:decorate id="yearBuilt" template="layout/display.xhtml">
            <ui:define name="label">yearBuilt</ui:define>
            #{courseHome.instance.yearBuilt}
        </s:decorate>

        <div style="clear:both"/>
        
    </rich:panel>
    
    <div class="actionButtons">      

        <s:button view="/CourseEdit.xhtml" 
                    id="edit" 
                 value="Edit"/>

        <s:button view="/#{empty courseFrom ? 'CourseList' : courseFrom}.xhtml"
                    id="done"
                 value="Done"/>

        <span style="padding-bottom: 10px; vertical-align: middle;">
				Course info:#{' '}
        <s:link view="/exportCourseInfo.xhtml"
                    id="exportCourseInfo"
                 value="PDF" target="_blank">
            <f:param name="courseId" value="#{courseHome.id}"/>
        </s:link>
        #{' '}
				Scorecard:#{' '}
        <s:link view="/exportSimpleScorecard.xhtml"
                    id="exportSimpleScorecard"
                 value="simple PDF" target="_blank">
            <f:param name="courseId" value="#{courseHome.id}"/>
        </s:link>
				#{' '}
        <s:link view="/exportScorecard.xhtml"
                    id="exportCompleteScorecard"
                 value="complete PDF" target="_blank">
            <f:param name="courseId" value="#{courseHome.id}"/>
        </s:link>
				#{' '}
				<s:span id="addToFavoritesLink" rendered="#{identity.loggedIn}">
					<a href="javascript: void(0);" onclick="addToFavorites(#{courseHome.id}, 'Course', #{currentGolfer.id});">Add to favorites</a>
				</s:span>
        </span>

    </div>
<h:form rendered="#{identity.loggedIn}">
Friend's email <h:inputText id="recipient" value="#{recipient.emailAddress}" required="true"><s:validate/></h:inputText> 
	 <h:commandButton action="#{notifications.sendScorecard}" value="Send to friend">
	 <f:setPropertyActionListener target="#{scorecard.courseId}" value="#{courseHome.id}"/>
	 </h:commandButton>
</h:form>

    <rich:tabPanel switchType="ajax">

    <rich:tab label="facility">
    <div class="association" id="facilityParent">
        
        <h:outputText value="No facility" 
                   rendered="#{courseHome.instance.facility == null}"/>
        
        <rich:dataTable var="facility" 
                   value="#{courseHome.instance.facility}" 
                rendered="#{courseHome.instance.facility != null}"
              rowClasses="rvgRowOne,rvgRowTwo"
                      id="facilityTable">
            <h:column>
                <f:facet name="header">id</f:facet>
                #{facility.id}
            </h:column>
            <h:column>
                <f:facet name="header">address</f:facet>
                #{facility.address}
            </h:column>
            <h:column>
                <f:facet name="header">city</f:facet>
                #{facility.city}
            </h:column>
            <h:column>
                <f:facet name="header">country</f:facet>
                #{facility.country}
            </h:column>
            <h:column>
                <f:facet name="header">county</f:facet>
                #{facility.county}
            </h:column>
            <h:column>
                <f:facet name="header">description</f:facet>
                #{facility.description}
            </h:column>
            <h:column>
                <f:facet name="header">drivingRange</f:facet>
                #{facility.drivingRange}
            </h:column>
            <h:column>
                <f:facet name="header">name</f:facet>
                #{facility.name}
            </h:column>
            <h:column>
                <f:facet name="header">phone</f:facet>
                #{facility.phone}
            </h:column>
            <h:column>
                <f:facet name="header">proShop</f:facet>
                #{facility.proShop}
            </h:column>
            <h:column>
                <f:facet name="header">rentsClubs</f:facet>
                #{facility.rentsClubs}
            </h:column>
            <h:column>
                <f:facet name="header">restaurant</f:facet>
                #{facility.restaurant}
            </h:column>
            <h:column>
                <f:facet name="header">snackBar</f:facet>
                #{facility.snackBar}
            </h:column>
            <h:column>
                <f:facet name="header">state</f:facet>
                #{facility.state}
            </h:column>
            <h:column>
                <f:facet name="header">type</f:facet>
                #{facility.type}
            </h:column>
            <h:column>
                <f:facet name="header">uri</f:facet>
                #{facility.uri}
            </h:column>
            <h:column>
                <f:facet name="header">priceRange</f:facet>
                #{facility.priceRange}
            </h:column>
            <h:column>
                <f:facet name="header">zip</f:facet>
                #{facility.zip}
            </h:column>
            <h:column>
                <f:facet name="header">action</f:facet>
                <s:link id="viewfacility" 
                     value="View" 
                      view="/Facility.xhtml">
                    <f:param name="facilityId" 
                           value="#{facility.id}"/>
                </s:link>
            </h:column>
        </rich:dataTable>
        
    </div>
    </rich:tab>

    <rich:tab label="holes">
    <div class="association" id="holesChildren">
        
        <h:outputText value="No holes" 
                   rendered="#{empty courseHome.holes}"/>
        
        <rich:dataTable value="#{courseHome.holes}" 
                       var="hole" 
                  rendered="#{not empty courseHome.holes}" 
                rowClasses="rvgRowOne,rvgRowTwo"
                        id="holesTable">
            <h:column>
                <f:facet name="header">id</f:facet>
                <h:outputText value="#{hole.id}"/>
            </h:column>
            <h:column>
                <f:facet name="header">ladiesHandicap</f:facet>
                <h:outputText value="#{hole.ladiesHandicap}"/>
            </h:column>
            <h:column>
                <f:facet name="header">ladiesPar</f:facet>
                <h:outputText value="#{hole.ladiesPar}"/>
            </h:column>
            <h:column>
                <f:facet name="header">mensHandicap</f:facet>
                <h:outputText value="#{hole.mensHandicap}"/>
            </h:column>
            <h:column>
                <f:facet name="header">mensPar</f:facet>
                <h:outputText value="#{hole.mensPar}"/>
            </h:column>
            <h:column>
                <f:facet name="header">number</f:facet>
                <h:outputText value="#{hole.number}"/>
            </h:column>
            <h:column>
                <f:facet name="header">action</f:facet>
                <s:link id="selecthole" 
                     value="Select" 
                      view="/Hole.xhtml">
                    <f:param name="holeId" 
                            value="#{hole.id}"/>
                    <f:param name="holeFrom" value="Course"/>
                </s:link>
            </h:column>
        </rich:dataTable>
        
    </div>
    
    <div class="actionButtons">
        <s:button 
               value="Add hole"
                view="/HoleEdit.xhtml">
            <f:param name="courseId" 
                    value="#{courseHome.instance.id}"/>
            <f:param name="holeFrom" value="Course"/>
        </s:button>
    </div>        
    </rich:tab>

    <rich:tab label="teeSets">
    <div class="association" id="teeSetsChildren">
        
        <h:outputText value="No teeSets" 
                   rendered="#{empty courseHome.teeSets}"/>
        
        <rich:dataTable value="#{courseHome.teeSets}" 
                       var="teeSet" 
                  rendered="#{not empty courseHome.teeSets}" 
                rowClasses="rvgRowOne,rvgRowTwo"
                        id="teeSetsTable">
            <h:column>
                <f:facet name="header">id</f:facet>
                <h:outputText value="#{teeSet.id}"/>
            </h:column>
            <h:column>
                <f:facet name="header">color</f:facet>
                <div title="#{teeSet.color}" style="background-color: #{teeSet.color}; height: 1em; width: 1em; outline: 1px solid black; margin: 0 auto;" />
            </h:column>
            <h:column>
                <f:facet name="header">ladiesCourseRating</f:facet>
                <h:outputText value="#{teeSet.ladiesCourseRating}"/>
            </h:column>
            <h:column>
                <f:facet name="header">ladiesSlopeRating</f:facet>
                <h:outputText value="#{teeSet.ladiesSlopeRating}"/>
            </h:column>
            <h:column>
                <f:facet name="header">mensCourseRating</f:facet>
                <h:outputText value="#{teeSet.mensCourseRating}"/>
            </h:column>
            <h:column>
                <f:facet name="header">mensSlopeRating</f:facet>
                <h:outputText value="#{teeSet.mensSlopeRating}"/>
            </h:column>
            <h:column>
                <f:facet name="header">name</f:facet>
                <h:outputText value="#{teeSet.name}"/>
            </h:column>
            <h:column>
                <f:facet name="header">position</f:facet>
                <h:outputText value="#{teeSet.position}"/>
            </h:column>
            <h:column>
                <f:facet name="header">action</f:facet>
                <s:link id="selectteeSet" 
                     value="Select" 
                      view="/TeeSet.xhtml">
                    <f:param name="teeSetId" 
                            value="#{teeSet.id}"/>
                    <f:param name="teeSetFrom" value="Course"/>
                </s:link>
            </h:column>
        </rich:dataTable>
        
    </div>
    
    <div class="actionButtons">
        <s:button 
               value="Add teeSet"
                view="/TeeSetEdit.xhtml">
            <f:param name="courseId" 
                    value="#{courseHome.instance.id}"/>
            <f:param name="teeSetFrom" value="Course"/>
        </s:button>
    </div>        
    </rich:tab>
</rich:tabPanel>
<div style="margin-top: 5px;"/>
<rich:tabPanel id="reviews" switchType="client" selectedTab="#{param.reviewTab}">
	<rich:tab id="list" label="Reviews (#{courseReviews.size})">
		<div class="reviews">
			<h:outputText value="This course has not yet been reviewed." rendered="#{courseReviews.size eq 0}"/>
			<a4j:repeat var="_review" value="#{courseReviews}" rowKeyVar="index">
				<s:div styleClass="review">
					<p style="font-weight: bold; text-transform: uppercase;">
						<s:graphicImage value="#{_review.reviewer.image}" style="vertical-align: middle; margin-right: 8px; position: relative; top: -2px;">
							<s:transformImageSize width="36" maintainRatio="true"/>
						</s:graphicImage>
						<s:link view="/profile.xhtml" value="#{_review.reviewer.username}">
							<f:param name="golferId" value="#{_review.reviewer.id}"/>
						</s:link>
						#{' | '}
						<strong>Rating</strong>#{' '}<h:graphicImage value="/img/rate-#{_review.rating}.gif" />
						#{' | '}
						<h:outputText value="#{_review.dateCreated}"><s:convertDateTime pattern="dd MMM yyyy"/></h:outputText>
					</p>
					<p><s:formattedText value="#{_review.text}" /></p>
				</s:div>
				<rich:separator height="1" rendered="#{index + 1 lt courseReviews.size}" lineType="dotted"/>
			</a4j:repeat>
		</div>
	</rich:tab>
	<rich:tab id="add" label="Add review">
		<!-- TODO: this needs to bring the user back here -->
		<s:fragment rendered="#{not identity.loggedIn}"><p>Please #{' '}<s:link view="/login.xhtml">login</s:link>#{' '} if you would like to add a review.</p></s:fragment>
		<h:form id="AddReview" rendered="#{identity.loggedIn}">
			<p>Please add your review in the form below. Please note that the comment field supports a shorthand syntax called "wiki text". You can use this syntax to format your entry.</p>
			<s:decorate id="textDecoration" template="layout/edit.xhtml">
				<ui:define name="label">comment</ui:define>
				<h:inputTextarea id="text" cols="80" rows="3" value="#{courseReviewHome.instance.text}" required="true">
					<s:validateFormattedText/>
				</h:inputTextarea>
			</s:decorate>
			<s:decorate id="ratingDecoration" template="layout/edit.xhtml">
				<ui:define name="label">rating</ui:define>
				<h:selectOneMenu id="rating" value="#{courseReviewHome.instance.rating}">
					<f:selectItem itemValue="" itemLabel=""/>
					<f:selectItem itemValue="0" itemLabel="0"/>
					<f:selectItem itemValue="1" itemLabel="1"/>
					<f:selectItem itemValue="2" itemLabel="2"/>
					<f:selectItem itemValue="3" itemLabel="3"/>
					<f:selectItem itemValue="4" itemLabel="4"/>
					<f:selectItem itemValue="5" itemLabel="5"/>
				</h:selectOneMenu>
			</s:decorate>
			<div style="clear:both"/>
			<!-- TODO: might want to make wiki help a toggle panel -->
			<div style="margin-top: 5px; padding: 5px 5px 0 5px; float: left; border: 1px solid black;">
				<strong style="text-decoration: underline;">Wiki Text - Quick Reference</strong>
				<pre>*bold* /italic/ |monospace| -strikethrough- ^super^ _underline_

+Big Heading
Headings must be followed by text

++Smaller heading
Paragraphs are ended with a blank line.

#ordered list item

=unordered list item

"quoted text"</pre>
			</div>
			<div style="clear:both"/>
			<div class="actionButtons">
				<h:commandButton id="save" value="Submit" action="#{courseReviewHome.persist}"/>
			</div>
		</h:form>
	</rich:tab>
</rich:tabPanel>
</ui:define>

</ui:composition>
